package com.yanqu.road.dao.impl.activity.pawnshop;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.pawnshop.UserPawnshopNotify;

public class UserPawnshopNotifyDaoImpl extends TempDataObjectDao<UserPawnshopNotify> {

	public static String maxUserPawnshopNotifySql = "select max(`notifyId`) as maxId from `t_u_activity_pawnshop_notify` where `userId`=? and `activityId`=?";

	public long getMaxUserPawnshopNotifyId(long userId, int activityId) {
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(maxUserPawnshopNotifySql);
			call.setLong(1, userId);
			call.setInt(2, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				return rs.getLong("maxId");
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return 0;
	}

	public static String getUserPawnshopNotifySql = "select * from `t_u_activity_pawnshop_notify` where `userId`=? and `activityId`=? and `isRead`=0";

	public Map<Long, UserPawnshopNotify> getUserPawnshopNotify(long userId, int activityId) {
		Map<Long, UserPawnshopNotify> map = new HashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserPawnshopNotifySql);
			int i = 1;
			call.setLong(i++, userId);
			call.setInt(i++, activityId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserPawnshopNotify data = parse(rs);
				map.put(data.getNotifyId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return map;
	}

	public static String updateUserPawnshopNotifySql = "update `t_u_activity_pawnshop_notify` set isRead=?,type=?,itemId=?,coinValuationOld=?," +
			"statusOld=?,redemptionPrice=? where userId=? and activityId=? and notifyId=?";

	@Override
	protected boolean updateData(UserPawnshopNotify userPawnshopNotify) {
		userPawnshopNotify.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopNotify.getIsRead()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getType()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getItemId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getCoinValuationOld()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getStatusOld()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getRedemptionPrice()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getNotifyId()));
		if (getDb().executeNoneQuery(updateUserPawnshopNotifySql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserPawnshopNotifySql = "insert into `t_u_activity_pawnshop_notify`(userId,activityId,notifyId,isRead,type,itemId," +
			"coinValuationOld,statusOld,redemptionPrice) values (?,?,?,?,?,?,?,?,?)";

	@Override
	protected boolean addData(UserPawnshopNotify userPawnshopNotify) {
		userPawnshopNotify.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getActivityId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getNotifyId()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userPawnshopNotify.getIsRead()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getType()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getItemId()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getCoinValuationOld()));
		para.put(i++, new DbParameter(Types.INTEGER, userPawnshopNotify.getStatusOld()));
		para.put(i++, new DbParameter(Types.BIGINT, userPawnshopNotify.getRedemptionPrice()));
		if (getDb().executeNoneQuery(insertUserPawnshopNotifySql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserPawnshopNotify parseData(ResultSet rs) throws SQLException {
		UserPawnshopNotify data = new UserPawnshopNotify();
		data.setUserId(rs.getLong("userId"));
		data.setActivityId(rs.getInt("activityId"));
		data.setNotifyId(rs.getLong("notifyId"));
		data.setIsRead(rs.getBoolean("isRead"));
		data.setType(rs.getInt("type"));
		data.setItemId(rs.getLong("itemId"));
		data.setCoinValuationOld(rs.getLong("coinValuationOld"));
		data.setStatusOld(rs.getInt("statusOld"));
		data.setRedemptionPrice(rs.getLong("redemptionPrice"));
		return data;
	}

}
